home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 14 / Example 14.1 / terrain.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-16  |  3.2 KB  |  140 lines

  1. #ifndef _TERRAIN_
  2. #define _TERRAIN_
  3.  
  4. #include <d3dx9.h>
  5. #include <vector>
  6. #include <fstream>
  7. #include "heightmap.h"
  8. #include "debug.h"
  9. #include "shader.h"
  10. #include "object.h"
  11.  
  12. class CAMERA;
  13. class MAPOBJECT;
  14.  
  15. struct TERRAINVertex
  16. {
  17.     TERRAINVertex(){}
  18.     TERRAINVertex(D3DXVECTOR3 pos, D3DXVECTOR3 norm, D3DXVECTOR2 _uv1, D3DXVECTOR2 _uv2)
  19.     {
  20.         position = pos;
  21.         normal = norm;
  22.         uv1 = _uv1;
  23.         uv2 = _uv2;
  24.     }
  25.  
  26.     D3DXVECTOR3 position, normal;
  27.     D3DXVECTOR2 uv1, uv2;
  28.  
  29.     static const DWORD FVF;
  30. };
  31.  
  32. struct PATCH{
  33.     PATCH();
  34.     ~PATCH();
  35.     void Release();
  36.     HRESULT CreateMesh(TERRAIN &ter, RECT source, IDirect3DDevice9* Dev);
  37.     void Render();
  38.  
  39.     IDirect3DDevice9* m_pDevice;
  40.     ID3DXMesh *m_pMesh;
  41.     RECT m_mapRect;
  42.     BBOX m_BBox;
  43.     bool m_visible;
  44. };
  45.  
  46. struct MAPTILE{
  47.     MAPTILE()    //Set everything to 0
  48.     {
  49.         m_type = m_set = 0; 
  50.         m_height = m_cost = 0.0f;
  51.         m_walkable = false;
  52.         m_pMapObject = NULL;
  53.         m_pParent = NULL;
  54.  
  55.         for(int i=0;i<8;i++)
  56.             neighbors[i] = NULL;
  57.     }
  58.  
  59.     int m_type, m_set;
  60.     float m_height, m_cost;
  61.     bool m_walkable;
  62.     MAPTILE* neighbors[8];
  63.     MAPOBJECT* m_pMapObject;
  64.  
  65.     // Pathfinding variables
  66.     INTPOINT m_mappos;
  67.     float f,g;
  68.     bool open, closed;
  69.     MAPTILE *m_pParent;
  70. };
  71.  
  72. class TERRAIN{
  73.     friend struct PATCH;
  74.     friend class MOUSE;
  75.     friend class CAMERA;
  76.     friend class APPLICATION;
  77.     public:
  78.         TERRAIN();        
  79.         void Init(IDirect3DDevice9* Dev, INTPOINT _size);
  80.         void Release();
  81.         void GenerateRandomTerrain(int numPatches);
  82.         void CreatePatches(int numPatches);
  83.         void CalculateAlphaMaps();
  84.         void CalculateLightMap(bool allWhite);
  85.         D3DXVECTOR3 GetNormal(int x, int y);
  86.         void AddObject(int type, INTPOINT mappos);
  87.         void Render(CAMERA &camera);        
  88.         void Progress(std::string text, float prc);
  89.         void SetOrthogonalView();
  90.         void UpdateSightMatrices(std::vector<MAPOBJECT*> &mapObjects);
  91.         void RenderLandscape();
  92.  
  93.         //Pathfinding
  94.         bool Within(INTPOINT p);    //Test if a point is within the bounds of the terrain
  95.         void InitPathfinding();
  96.         void UpdatePathfinding(RECT *r);
  97.         void CreateTileSets();
  98.         std::vector<INTPOINT> GetPath(INTPOINT start, INTPOINT goal, bool considerUnits, MAPOBJECT *unit);
  99.         MAPTILE* GetTile(int x, int y);
  100.         MAPTILE* GetTile(INTPOINT p){return GetTile(p.x, p.y);}
  101.         D3DXVECTOR3 GetWorldPos(INTPOINT mappos);
  102.         INTPOINT GetClosestFreeTile(INTPOINT to, INTPOINT from);
  103.         bool PositionAccessible(MAPOBJECT *unit, INTPOINT position);
  104.  
  105.         //Save and Load Map
  106.         void SaveTerrain(char fileName[]);
  107.         void LoadTerrain(char fileName[]);
  108.  
  109.         //Public variables
  110.         MAPTILE *m_pMapTiles;
  111.         bool *m_pVisitedTiles, *m_pVisibleTiles;
  112.         bool m_updateSight;
  113.  
  114.         IDirect3DTexture9* m_pFogOfWarTexture;
  115.         IDirect3DTexture9* m_pLightMap;
  116.         IDirect3DTexture9* m_pLandScape;
  117.         INTPOINT m_size;
  118.  
  119.     private:
  120.         
  121.         IDirect3DDevice9* m_pDevice; 
  122.         ID3DXFont *m_pProgressFont;
  123.  
  124.         HEIGHTMAP *m_pHeightMap;
  125.         std::vector<PATCH*> m_patches;
  126.         std::vector<IDirect3DTexture9*> m_diffuseMaps;
  127.         std::vector<OBJECT> m_objects;
  128.         IDirect3DTexture9* m_pAlphaMap;        
  129.  
  130.         SHADER m_terrainPS, m_terrainVS;
  131.         SHADER m_objectsPS, m_objectsVS;
  132.  
  133.         D3DXVECTOR3 m_dirToSun;
  134.         D3DXHANDLE m_vsMatW, m_vsMatVP, m_vsDirToSun;
  135.         D3DXHANDLE m_objMatW, m_objMatVP, m_objDirToSun, m_objMapSize;
  136.  
  137.         D3DMATERIAL9 m_mtrl;
  138. };
  139.  
  140. #endif